//
//  XMTableViewController.m
//  XMFriendCircleDemo
//
//  Created by chenming on 2018/1/26.
//  Copyright © 2018年 ChenXiaoMing. All rights reserved.
//

#import "XMTableViewController.h"

// Models
#import "XMFriendCircleModel.h"

// Views
#import "XMTableViewCellHeaderView.h"
#import "XMTableViewCellFooterView.h"
#import "XMCommentTableViewCell.h"
#import "XMCoreTextLabel.h"

// Controllers

// Tool
#import "XMTableViewFPSLabel.h"

@interface XMTableViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) XMTableViewCellHeaderView *headerView;

@property (nonatomic, strong) NSMutableDictionary *dictSection;

@end

static NSString *const cellID = @"cellID";

@implementation XMTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    if (@available(iOS 11.0, *)) {
        self.tableView.estimatedRowHeight = 0;
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        self.tableView.layoutMargins = UIEdgeInsetsZero;
    }
    
    self.tableView.estimatedSectionHeaderHeight = 180;
    self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(self.view).offset(0);
    }];

//    [self.view setNeedsLayout];
//    [self.view layoutIfNeeded];
    
    XMTableViewFPSLabel *label = [[XMTableViewFPSLabel alloc] init];
    label.frame = CGRectMake(kSCREEN_WIDTH - 85, 10, 75, 30);
    [self.view addSubview:label];
    [self.view bringSubviewToFront:label];
    
    [self.dataSource addObjectsFromArray:[self creatModelsWithCount:20]];
    [self.tableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationItem.title = @"TableView朋友圈";
}

- (NSArray *)creatModelsWithCount:(NSInteger)count
{
    NSArray *iconImageNamesArray = @[@"http://bpic.588ku.com/element_origin_min_pic/17/03/15/ce9fdd86c19f1f9b8862ce237fd19133.jpg",
                                     @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1517107848643&di=118fcb942860b8646644a97f973ae047&imgtype=0&src=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201510%2F31%2F20151031104733_dxQKz.thumb.700_0.png",
                                     @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1517107931713&di=67b00d7a0b883171a59f0b21cc640923&imgtype=0&src=http%3A%2F%2Fpic3.16pic.com%2F00%2F55%2F07%2F16pic_5507165_b.jpg",
                                     @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1517107969070&di=4126a5db5123786d387cea25770aee45&imgtype=0&src=http%3A%2F%2Fc.hiphotos.baidu.com%2Fbaike%2Fpic%2Fitem%2Fae51f3deb48f8c541efbf9613a292df5e0fe7f57.jpg",
                                     @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1517108009822&di=60f125cceca88ecb66225e680940a960&imgtype=0&src=http%3A%2F%2Fimg.sgamer.com%2Fdota2_sgamer_com%2Fimages%2F20140208%2Fb1061716e737062c0c84c06d4ec7032c.jpg",
                                     @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1517108091327&di=486d1e6f31325bf1948601c5f5697366&imgtype=0&src=http%3A%2F%2Fimg.tupianzj.com%2Fuploads%2Fallimg%2F160812%2F9-160Q2214643.jpg"];
    
    NSArray *namesArray = @[@"陈小明",
                            @"风口上的猪",
                            @"当今世界网名都不好起了",
                            @"我叫郭德纲",
                            @"Hello Kitty",
                            @"蜡笔小新"];
    
    NSArray *textArray = @[@"霜风萧厉襟寒透，放眼望，满山青秀。莫恨雪飞迟，秋去冬来久.",
                           @"雪飞总在深寒后，且负手，殷勤等候。看水逝云流，只有风依旧.",
                           @"小时候，我盼望着过年掰着手指，数着星星觉得一年好长，好长每到过年，我总是羡慕邻居家姐姐的花鞋，花衣裳 长大了我盼望着过年过年可以松闲几天过年可以回家看看母亲腌的酸菜父亲舂的年.",
                           @"你也许奈不住山的沉寂， 我厌倦繁华都市的喧嚣， 在一个开满花的季节里， 你我在山间小路上相遇。 你凝望着我，充满好奇， 我打量着你，不觉灵犀， 你眼神中仿佛带有怨气， 俩个酷.",
                           @"“我可太知道了。我打电话到雨晨学校去找她。这学校老师说，她都一个月没来上课了。我赶紧跟她这接电话的老师说，这孩子可能出事儿了，这才赶紧让报的警！”朱喜芬一字一顿，眉头紧蹙着说.💢💢💢",
                           @"“你看看这院子里牡丹花都是她给我架起来的。平时啊，我有个头疼脑热的，我就给她打电话。她都陪我去医院。她几乎啊，每天都得来看看我，可是就这一个月，这孩子就彻底看不到影儿了。我给她拨了电话，她也没有接。这可不是雨晨干的事儿啊，这小区里的老住户们，谁不知道雨晨啊，这可是个好孩子啊。”朱喜芬说着声音有点哽咽，带着哭腔。👌👌👌👌"
                           ];
    
    
    NSArray *timesArray = @[   @"1小时前",
                               @"15分钟前",
                               @"5分钟前",
                               @"50分钟前",
                               @"2小时前",
                               @"4小时前",
                               ];
    
    NSArray *picImageArray = @[@"http://bpic.588ku.com/back_pic/05/34/95/325a300a64da21d.jpg!ww650",
                               @"http://bpic.588ku.com/back_pic/05/29/55/785a0fc62156485.jpg!ww800",
                               @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1517161684325&di=b95ca1042f39d4ec4f6902c16dde41aa&imgtype=0&src=http%3A%2F%2Fjoymepic.joyme.com%2Farticle%2Fuploads%2F20174%2F191495187281991442.jpeg",
                               @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1517161718798&di=2b498a6079338980c083f8bcf08a9925&imgtype=0&src=http%3A%2F%2Fbcs.91.com%2Frbpiczy%2FWallpaper%2F2015%2F1%2F9%2Fd4b1ade76d3549bdbd94937201a32c0d-9.jpg",
                               @"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1999728452,2184696721&fm=27&gp=0.jpg",
                               @"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1045510890,38594538&fm=27&gp=0.jpg",
                               @"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=589681912,1937835127&fm=27&gp=0.jpg",
                               @"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=347553751,1010600552&fm=27&gp=0.jpg",
                               @"https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=175081137,178068070&fm=27&gp=0.jpg"
                               ];
    NSMutableArray *resArr = [NSMutableArray new];
    for (int i = 0; i < count; i++) {
        int iconRandomIndex = arc4random_uniform(6);
        int nameRandomIndex = arc4random_uniform(6);
        int contentRandomIndex = arc4random_uniform(6);
        int timeRandomIndex = arc4random_uniform(6);
        
        XMFriendCircleModel *model = [[XMFriendCircleModel alloc] init];
        model.icon = iconImageNamesArray[iconRandomIndex];
        model.name = namesArray[nameRandomIndex];
        model.content = textArray[contentRandomIndex];
        model.time = timesArray[timeRandomIndex];
        
        // 模拟“随机图片”
        int random = arc4random_uniform(6);
        NSMutableArray *temp = [NSMutableArray new];
        for (int i = 0; i < random; i++) {
            int randomIndex = arc4random_uniform(9);
            [temp addObject:picImageArray[randomIndex]];
        }
        if (temp.count) {
            model.picImagesArray = [temp copy];
        }

        [resArr addObject:model];
    }
    return [resArr copy];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    _headerView = [XMTableViewCellHeaderView headerViewWithTableView:tableView];
    _headerView.tableView = self.tableView;
    _headerView.section = section;
    _headerView.model = self.dataSource[section];
    return _headerView;
}


- (CGFloat)tableViewHeaderSection:(NSString *)title titleFontSize:(CGFloat)titleFontSize {
    CGFloat height = [title boundingRectWithSize:CGSizeMake(kSCREEN_WIDTH - 70, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:titleFontSize]} context:nil].size.height;
    return height;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[XMCommentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.tableView = tableView;
    cell.section = indexPath.section;
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

- (NSMutableDictionary *)dictSection {
    if (!_dictSection) {
        _dictSection = [[NSMutableDictionary alloc] init];
    }
    return _dictSection;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
